package com.gitee.chili.leisure.service.impl;

import com.gitee.chili.leisure.Tools;
import com.gitee.chili.leisure.domain.Account;
import com.gitee.chili.leisure.domain.Currency;
import com.gitee.chili.leisure.domain.EntryRoom;
import com.gitee.chili.leisure.domain.Member;
import com.gitee.chili.leisure.domain.dto.MemberVo;
import com.gitee.chili.leisure.repository.MemberRepository;
import com.gitee.chili.leisure.service.IAccountService;
import com.gitee.chili.leisure.service.IEntryRoomService;
import com.gitee.chili.leisure.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * MemberService
 *
 * @author week solider
 * @since 2020.09.01
 */
@Service
public class MemberService implements IMemberService {

    @Autowired
    private MemberRepository memberRepository;

    @Autowired
    private IEntryRoomService entryRoomService;

    @Autowired
    private IAccountService accountService;

    @Override
    public MemberRepository getRepository() {
        return memberRepository;
    }

    @Override
    public Account getAccount(Member member, Currency currency) {

        // 获取资产
        Optional<Account> optionalAccount = member.getAccounts().stream()
                .filter(account -> account.getCurrency().getToken().equals(currency.getToken())).findFirst();
        return optionalAccount.orElse(null);
    }

    @Override
    public Member query(String phone) {
        return memberRepository.findByPhone(phone);
    }

    @Override
    public Member query(String username, String password) {
        return memberRepository.findByUsernameAndPassword(username, password);
    }

    @Override
    public EntryRoom disconnection(Long memberId) {
        return entryRoomService.roomInside(memberId);
    }

    @Override
    public MemberVo builder(Member member) {
        MemberVo vo = new MemberVo();

        // 断线判断
        EntryRoom entryRoom = disconnection(member.getId());

        // 这时候需要断线重连
        if (Objects.nonNull(entryRoom)) {
            vo.setRoomNum(entryRoom.getRoom().getRoomNum())
                    .setRoomId(entryRoom.getRoom().getId());
        }

        // 获取账户可用资产
        if (Objects.nonNull(member.getAccounts()) && !member.getAccounts().isEmpty()) {
            member.getAccounts().forEach(account -> {
                switch (account.getCurrency().getToken()) {
                    case GAME:
                        vo.setScore(Tools.rmbCast(account.getBalance()));
                        break;
                    case GEMS:
                        vo.setGems(Tools.rmbCast(account.getBalance()));
                        break;
                    case RMB:
                        vo.setBalance(Tools.rmbCast(account.getBalance()));
                        break;
                }
            });
        }

        return vo.setAreaProvince(member.getAreaProvince())
                .setNickname(member.getNickname())
                .setAreaCity(member.getAreaCity())
                .setPortrait(member.getPortrait())
                .setGender(member.getGender())
                .setLevel(member.getLevel())
                .setExp(member.getExp());
    }
}
